package dao.impl;

import dao.BaseDao;
import dao.ICartDao;
import pojo.Cart;

import java.util.List;

public class CartDaoImpl extends BaseDao implements ICartDao {
    /**
     * 添加到购物车
     *
     * @param cart
     * @return
     */
    @Override
    public boolean insertToCart(Cart cart) {
        return update("insert into t_cart value(?,?,?)", cart.getUid(), cart.getGid(), cart.getG_count()) == 1;
    }

    /**
     * 根据uid和gid查询购物车数据
     *
     * @param uid
     * @param gid
     * @return
     */
    @Override
    public Cart queryByUidAndGid(int uid, int gid) {
        return queryForOne(Cart.class, "select uid,gid,g_count from t_cart where uid=? and gid=?", uid, gid);
    }

    /**
     * 根据uid查询购物车数据
     *
     * @param uid
     * @return List<Cart>
     */
    @Override
    public List<Cart> queryByUid(int uid) {
        return queryForList(Cart.class, "select uid,gid,g_count from t_cart where uid=?", uid);
    }

    /**
     * 根据uid和gid更新购物车的商品数量
     *
     * @param uid
     * @param gid
     * @return
     */
    @Override
    public boolean updateCountByUidAndGid(int addCount, int uid, int gid) {
        return update("update t_cart set g_count=g_count+? where uid=? and gid=?", addCount, uid, gid) == 1;
    }

    /**
     * 根据uid清空购物车
     *
     * @param uid
     * @return 改变行数
     */
    @Override
    public int clearCartByUid(int uid) {
        return update("delete from t_cart where uid=?", uid);
    }

    /**
     * 根据uid、gid从购物车中删除数据
     *
     * @param gid
     * @return 改变行数
     */
    @Override
    public int removeFromCartByUidAndGid(int uid, int gid) {
        return update("delete from t_cart where uid=? and gid=?",uid, gid);
    }
}
